clear all
global system "linux"
set more off
set matsize 4000

if "${system}" == "linux" {
	*global code "/"
	global code "/"
	global s "/"
}

run "${code}${s}_set-path.do"

args index
cap log close _all
log using "${log}/log-jc-exogvars-`index'", name("log`index'") text replace


if `index' == 1 local DepVar "credit_score"
if `index' == 2 local DepVar "mtoi"
if `index' == 3 local DepVar "BrokerOrig"
if `index' == 4 local DepVar "CorrespondentOrig"
if `index' == 5 local DepVar "MultiBorrower"
if `index' == 6 local DepVar "Freddie"


*** Load data
use "${temp}/embs-lps-matched.dta", clear  // 12c-embs-lps-match.do


// specifications
do "_embs-lps-spec.do"




*** Polynomials
gen PaboveC1 = PaboveC
gen PaboveC2 = PaboveC * PaboveC 
gen PaboveC3 = PaboveC2* PaboveC


global poly1 "PaboveC 1.HiP#c.PaboveC1"
global poly2 "$poly1 PaboveC2 1.HiP#c.PaboveC2"
global poly3 "$poly2 PaboveC3 1.HiP#c.PaboveC3"




*** Plot
local Poly $poly3
reghdfe `DepVar' HiP `Poly', a(FE1=i.prop_zip_code FE2=i.orig_ym FE3=i.seller_id_id) vce(robust)
estimate save "${Estimates}/embs-lps-exogvars-`index'", replace

predict resid, r
drop FE* 
foreach x in HiP `Poly' {
	replace resid = resid + _b[`x']* (`x')
}

qui su `DepVar' if HiP==0
replace resid = resid + `r(mean)'

qui do "${code}/_labels"
local title: variable label `DepVar'


replace PaboveC = ceil(PaboveC/2.5)*2.5
collapse resid, by(PaboveC)


twoway scatter resid PaboveC, xline(0) saving("${Estimates}/embs-lps-exogvars-`index'.gph", replace) ///
 title("`title'") ytitle("") xtitle("")  xlabel(-150(50)150) ylabel(, angle(0))


cap log close _all
